home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1997 January: Mac OS SDK / Dev.CD Jan 97 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / ODF / OS / FWGraphx / FWPictur.cpp < prev    next >
Encoding:
Text File  |  1996-09-17  |  11.8 KB  |  438 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                FWPictur.cpp
  4. //    Release Version:    $ ODF 2 $
  5. //
  6. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  7. //
  8. //========================================================================================
  9.  
  10. #include "FWOS.hpp"
  11.  
  12. #ifndef FWPICTUR_H
  13. #include "FWPictur.h"
  14. #endif
  15.  
  16. #ifndef SLGRGLOB_H
  17. #include "SLGrGlob.h"
  18. #endif
  19.  
  20. #ifndef FWGRUTIL_H
  21. #include "FWGrUtil.h"
  22. #endif
  23.  
  24. #ifndef FWGC_H
  25. #include "FWGC.h"
  26. #endif
  27.  
  28. #ifndef FWODGEOM_H
  29. #include "FWODGeom.h"
  30. #endif
  31.  
  32. // ----- OS Includes -----
  33.  
  34. #ifndef FWRESACC_H
  35. #include "FWResAcc.h"
  36. #endif
  37.  
  38. #ifndef FWRESSIN_H
  39. #include "FWResSin.h"
  40. #endif
  41.  
  42. #ifndef FWACQUIR_H
  43. #include "FWAcquir.h"
  44. #endif
  45.  
  46. // ----- Foundation Includes -----
  47.  
  48. #ifndef FWSOMENV_H
  49. #include "FWSOMEnv.h"
  50. #endif
  51.  
  52. #ifndef FWSTREAM_H
  53. #include "FWStream.h"
  54. #endif
  55.  
  56. #ifndef FWDEBUG_H
  57. #include "FWDebug.h"
  58. #endif
  59.  
  60. #ifndef FWSTRMRW_H
  61. #include "FWStrmRW.h"
  62. #endif
  63.  
  64. #ifndef FWMEMMGR_H
  65. #include "FWMemMgr.h"
  66. #endif
  67.  
  68. // ----- OpenDoc Includes -----
  69.  
  70. #ifndef SOM_ODCanvas_xh
  71. #include <Canvas.xh>
  72. #endif
  73.  
  74. #ifndef SOM_ODShape_xh
  75. #include <Shape.xh>
  76. #endif
  77.  
  78. #ifndef SOM_ODTransform_xh
  79. #include <Trnsform.xh>
  80. #endif
  81.  
  82. // ----- Macintosh Includes -----
  83.  
  84. #if defined(FW_BUILD_MAC) && !defined(__RESOURCES__)
  85. #include <Resources.h>
  86. #endif
  87.  
  88. //========================================================================================
  89. //    RunTime Info
  90. //========================================================================================
  91.  
  92. #ifdef FW_BUILD_MAC
  93. #pragma segment FWGraphics_Picture
  94. #endif
  95.  
  96. //========================================================================================
  97. //    Template instantiation
  98. //========================================================================================
  99.  
  100. #include "FWGrRef.tpp"
  101.  
  102. FW_DEFINE_AUTO_TEMPLATE(FW_TGrRefPtr, FW_HPicture)
  103.  
  104. #ifdef FW_USE_TEMPLATE_PRAGMAS
  105.  
  106. #pragma template_access public
  107. #pragma template FW_TGrRefPtr<FW_HPicture>
  108.  
  109. #else
  110.  
  111. template class FW_TGrRefPtr<FW_HPicture>;
  112.  
  113. #endif
  114.  
  115. //========================================================================================
  116. //    class FW_CPicture
  117. //========================================================================================
  118.  
  119. FW_DEFINE_AUTO(FW_CPicture)
  120.  
  121. //----------------------------------------------------------------------------------------
  122. //    FW_PrivAcquireGrRep
  123. //----------------------------------------------------------------------------------------
  124.  
  125. void FW_PrivAcquireGrRep(FW_HPicture rep)
  126. {
  127.     if (rep != 0)
  128.         FW_PrivPicture_Acquire(rep);
  129. }
  130.  
  131. //----------------------------------------------------------------------------------------
  132. //    FW_PrivReleaseGrRep
  133. //----------------------------------------------------------------------------------------
  134.  
  135. void FW_PrivReleaseGrRep(FW_HPicture rep)
  136. {
  137.     if (rep != 0)
  138.         FW_PrivPicture_Release(rep);
  139. }
  140.  
  141. //----------------------------------------------------------------------------------------
  142. //    FW_CPicture::FW_CPicture
  143. //----------------------------------------------------------------------------------------
  144.  
  145. FW_CPicture::FW_CPicture()
  146. {
  147.     FW_END_CONSTRUCTOR
  148. }
  149.  
  150. //----------------------------------------------------------------------------------------
  151. //    FW_CPicture::FW_CPicture
  152. //----------------------------------------------------------------------------------------
  153.  
  154. FW_CPicture::FW_CPicture(FW_PlatformPict platformPict)
  155. {
  156.     FW_PlatformError error;
  157.     FW_HPicture rep = FW_PrivPicture_CreateFromPlatformPict(platformPict, &error);
  158.     FW_FailOnError(error);
  159.     SetRep(rep);
  160.     FW_END_CONSTRUCTOR
  161. }
  162.  
  163. //----------------------------------------------------------------------------------------
  164. //    FW_CPicture::FW_CPicture
  165. //----------------------------------------------------------------------------------------
  166.  
  167. FW_CPicture::FW_CPicture(FW_PResourceFile& resourceFile, FW_ResourceID resourceID)
  168. {
  169.     FW_PlatformError error;
  170.     FW_HPicture rep = FW_PrivPicture_CreateFromResource(resourceFile, resourceID, &error);
  171.     FW_FailOnError(error);
  172.     SetRep(rep);
  173.     FW_END_CONSTRUCTOR
  174. }
  175.  
  176. //----------------------------------------------------------------------------------------
  177. //    FW_CPicture::FW_CPicture
  178. //----------------------------------------------------------------------------------------
  179.  
  180. FW_CPicture::FW_CPicture(const FW_CPicture& other) :
  181.     FW_TGrRefPtr<FW_HPicture>(other)
  182. {
  183.     FW_END_CONSTRUCTOR
  184. }
  185.  
  186. //----------------------------------------------------------------------------------------
  187. //    FW_CPicture::FW_CPicture
  188. //----------------------------------------------------------------------------------------
  189.  
  190. FW_CPicture::FW_CPicture(FW_CReadableStream& stream)
  191. {
  192.     FW_PlatformError error;
  193.     FW_HPicture rep = FW_PrivPicture_Read(stream, &error);
  194.     FW_FailOnError(error);
  195.     SetRep(rep);
  196.     FW_END_CONSTRUCTOR
  197. }
  198.  
  199. //----------------------------------------------------------------------------------------
  200. //    FW_CPicture::~FW_CPicture
  201. //----------------------------------------------------------------------------------------
  202.  
  203. FW_CPicture::~FW_CPicture()
  204. {
  205.     FW_START_DESTRUCTOR
  206. }
  207.  
  208. //----------------------------------------------------------------------------------------
  209. //    FW_CPicture::operator=
  210. //----------------------------------------------------------------------------------------
  211.  
  212. FW_CPicture& FW_CPicture::operator=(const FW_CPicture& other)
  213. {
  214.     FW_TGrRefPtr<FW_HPicture>::operator=(other);
  215.     return *this;
  216. }
  217.  
  218. //----------------------------------------------------------------------------------------
  219. //    FW_CPicture::Copy
  220. //----------------------------------------------------------------------------------------
  221.  
  222. FW_CPicture FW_CPicture::Copy() const
  223. {
  224.     FW_PlatformError error;
  225.     FW_HPicture rep = FW_PrivPicture_Copy(fRep, &error);
  226.     FW_FailOnError(error);
  227.     
  228.     FW_CPicture picture;
  229.     picture.SetRep(rep);
  230.     return picture;
  231. }
  232.  
  233. //----------------------------------------------------------------------------------------
  234. //    FW_CPicture::Copy
  235. //----------------------------------------------------------------------------------------
  236.  
  237. void FW_CPicture::GetPictBounds(FW_SGraphicContext& gc, FW_SRect& bounds) const
  238.     FW_PrivPicture_GetPictBoundsGC(gc.fEnvironment, fRep, gc, bounds); 
  239.     FW_FailOnEvError(gc.fEnvironment);
  240. }
  241.  
  242. //----------------------------------------------------------------------------------------
  243. //    FW_CPicture::SetPlatformPict
  244. //----------------------------------------------------------------------------------------
  245.  
  246. void FW_CPicture::SetPlatformPict(FW_PlatformPict newPict)
  247. {
  248.     if (!fRep)
  249.     {
  250.         FW_PlatformError error;
  251.         FW_HPicture rep = FW_PrivPicture_CreateFromPlatformPict(newPict, &error);
  252.         FW_FailOnError(error);
  253.         SetRep(rep);
  254.     }
  255.     else
  256.         FW_PrivPicture_SetPlatformPict(fRep, newPict);
  257. }
  258.  
  259. //----------------------------------------------------------------------------------------
  260. //    FW_CPicture::AdoptPlatformPict
  261. //----------------------------------------------------------------------------------------
  262.  
  263. void FW_CPicture::AdoptPlatformPict(FW_PlatformPict newPict)
  264. {
  265.     if (!fRep)
  266.     {
  267.         FW_PlatformError error;
  268.         FW_HPicture rep = FW_PrivPicture_CreateFromPlatformPict(newPict, &error);
  269.         FW_FailOnError(error);
  270.         SetRep(rep);
  271.     }
  272.     
  273.     // call adopt whether or not we originally had a rep. this will
  274.     // cause the adoption of ownership
  275.     FW_PrivPicture_AdoptPlatformPict(fRep, newPict);
  276. }
  277.  
  278.  
  279. //========================================================================================
  280. //    Stream I/O
  281. //========================================================================================
  282.  
  283. //----------------------------------------------------------------------------------------
  284. //    operator>>
  285. //----------------------------------------------------------------------------------------
  286.  
  287. FW_CReadableStream& operator>>(FW_CReadableStream& stream, FW_CPicture& picture)
  288. {
  289.     FW_PlatformError error;
  290.     FW_HPicture rep = FW_PrivPicture_Read(stream, &error);
  291.     FW_FailOnError(error);
  292.     picture.SetRep(rep);
  293.     return stream;
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. //    operator<<
  298. //----------------------------------------------------------------------------------------
  299.  
  300. FW_CWritableStream& operator<<(FW_CWritableStream& stream, const FW_CPicture& picture)
  301. {
  302.     FW_PlatformError error;
  303.     FW_PrivPicture_Write(picture.fRep, stream, &error);
  304.     FW_FailOnError(error);
  305.     return stream;
  306. }
  307.  
  308. //========================================================================================
  309. //    class FW_CPictureContext
  310. //========================================================================================
  311.  
  312. FW_DEFINE_AUTO(FW_CPictureContext)
  313.  
  314. //----------------------------------------------------------------------------------------
  315. //    FW_CPictureContext::FW_CPictureContext
  316. //----------------------------------------------------------------------------------------
  317.  
  318. FW_CPictureContext::FW_CPictureContext(Environment* ev,
  319.                                        FW_CPicture& picture,
  320.                                        FW_Fixed xSize,
  321.                                        FW_Fixed ySize) :
  322.     FW_CGraphicContext(ev),
  323. #ifdef FW_BUILD_MAC
  324.     fMacPictHandle(NULL),
  325. #endif
  326.     fPicture(picture),
  327.     fGraphicDevice(NULL)
  328. {
  329.     ODPlatformCanvas platformCanvas;
  330.  
  331.     FW_CRect clipRect;
  332.     
  333. #ifdef FW_BUILD_MAC
  334.     platformCanvas = FW_gScratchPort;
  335.     clipRect.Set(FW_IntToFixed(0), FW_IntToFixed(0), xSize, ySize);
  336. #endif
  337.  
  338. #ifdef FW_BUILD_WIN
  339.     HDC hDCRef = ::GetDC(NULL);
  340.     
  341.     int xRes = ::GetDeviceCaps(hDCRef, LOGPIXELSX);
  342.     int yRes = ::GetDeviceCaps(hDCRef, LOGPIXELSY);
  343.     
  344.     int xDevMM = ::GetDeviceCaps(hDCRef, HORZSIZE);
  345.     int yDevMM = ::GetDeviceCaps(hDCRef, VERTSIZE);
  346.     int xDevPix = ::GetDeviceCaps(hDCRef, HORZRES);
  347.     int yDevPix = ::GetDeviceCaps(hDCRef, VERTRES);
  348.  
  349.     // The size comes in at 72dpi: convert to HiMetric units (0.01 mm)
  350.     FW_CPlatformRect boundsMM(0, 0, FW_FixedToInt(xSize) * 2540 / 72, FW_FixedToInt(ySize) * 2540 / 72);
  351.  
  352.     platformCanvas = ::CreateEnhMetaFile(hDCRef, NULL, &boundsMM, NULL);
  353.  
  354.     // Convert size to pixels, for use in the coordinate system
  355.     int xSizePix = 
  356.         FW_FixedToInt(FW_WideMultiply(xSize, FW_IntToFixed(xRes)) / FW_kFixed72);
  357.  
  358.     int ySizePix =
  359.         FW_FixedToInt(FW_WideMultiply(ySize, FW_IntToFixed(yRes)) / FW_kFixed72);
  360.  
  361.     int xDraw = (254 * xSizePix * xDevPix) / (100 * xDevMM);
  362.     int yDraw = (254 * ySizePix * yDevPix) / (100 * yDevMM);
  363.  
  364.     clipRect.SetInt(0, 0, xDraw, yDraw);
  365.  
  366.     ::ReleaseDC(NULL, hDCRef);
  367. #endif
  368.  
  369.     fGraphicDevice = FW_PrivGDev_CreateFromPlatformCanvas(ev, platformCanvas);
  370.     FW_FailOnEvError(ev);
  371.  
  372. #ifdef FW_BUILD_WIN
  373.     // Set the resolution: (xSize, ySize) should map to (xDraw, yDraw)
  374.     FW_PrivGDev_SetResolution(fGraphicDevice,
  375.         FW_IntToFixed(boundsMM.right) / FW_IntToFixed(xDraw),
  376.         FW_IntToFixed(boundsMM.bottom) / FW_IntToFixed(yDraw)
  377.         );
  378. #endif
  379.  
  380.     FW_CAcquiredODShape aqClipShape = ::FW_NewODShape(ev, clipRect);
  381.  
  382.     InitGraphicContext(fGraphicDevice,
  383.                        NULL,
  384.                        aqClipShape);
  385.  
  386. #ifdef FW_BUILD_MAC
  387.     FW_CPlatformRect plfmBounds(0, 0, FW_FixedToInt(xSize), FW_FixedToInt(ySize));
  388.  
  389.     ::PortSize(plfmBounds.right, plfmBounds.bottom);
  390.     fMacPictHandle = ::OpenPicture(&plfmBounds);
  391. #endif
  392.  
  393.     FW_END_CONSTRUCTOR
  394. }
  395.  
  396. //----------------------------------------------------------------------------------------
  397. //    FW_CPictureContext::~FW_CPictureContext
  398. //----------------------------------------------------------------------------------------
  399.  
  400. FW_CPictureContext::~FW_CPictureContext()
  401. {
  402.     FW_START_DESTRUCTOR
  403.  
  404.     FW_PlatformPict newPict;
  405.  
  406. #ifdef FW_BUILD_MAC
  407.     ::ClosePicture();
  408.     newPict = fMacPictHandle;
  409. #endif
  410.  
  411. #ifdef FW_BUILD_WIN
  412.     HDC hDC = FW_PrivGDev_GetPlatformCanvas(fGraphicDevice);
  413. #endif
  414.  
  415.     // ----- Terminate the superclass
  416.     TerminateGraphicContext();
  417.  
  418. #ifdef FW_BUILD_WIN
  419.     newPict = ::CloseEnhMetaFile(hDC);
  420. #endif
  421.  
  422.     // ----- Delete the device
  423.     FW_PrivGDev_Release(fGraphicDevice);
  424.  
  425.     // ----- Store the result in the picture
  426.     fPicture.AdoptPlatformPict(newPict);
  427. }
  428.  
  429. //----------------------------------------------------------------------------------------
  430. //    FW_CPictureContext::Reset
  431. //----------------------------------------------------------------------------------------
  432.  
  433. void FW_CPictureContext::Reset()
  434. {
  435.     // Nothing to do [HLX] Am i sure ?
  436. }
  437.